// miniprogram/pages/main/main.js
const app = getApp();
var mainPageViewModel = null;
var _contentNavigationService = require("../../services/contentNavigationService.js");
var _moodStorage = require("../../services/MoodStorage.js");
var _heWeather=require("../../services/heWeather.js");
var _timeStamp = require("../../services/timeStamp.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    ColorList: app.globalData.ColorList,
    InitializePage:app.globalData.InitializePage,  
    Height:app.globalData.Height,  
    userName:app.globalData.userName,
    year: new Date().getFullYear(),      // 年份
    month: new Date().getMonth() + 1,    // 月份
    day: new Date().getDate(),
    days_color: [] ,
    weather:"",   
    moodItem:{},
  },

  button_bindtap: function () {
    _contentNavigationService.navigateToAsync(
      _contentNavigationService.writePage
    )
  },
  stati_bindtap: function () {
    _contentNavigationService.navigateToAsync(
      _contentNavigationService.statisticsPage
    )
  },
  getName_bindtap:function(e){
    this.setData({
      userName:e.detail.value,
    })
  },
  initialize_bindtap:function(){
    var that=this;
    if(that.data.userName!="")
    {
      wx.setStorageSync('userName',that.data.userName)
    }
    that.setData({
      InitializePage:false,
      userName:that.data.userName,
    })
  },
  text_animation:function(){

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const days_count = new Date(this.data.year, this.data.month, 0).getDate();
    let days_color = new Array;
    var that = this;
    var month = this.data.month.toString()
    if (this.data.month < 10) {
      month = "0" + month;
    }
    _moodStorage.getMonthRecordAsync(month, function (data) {

      for (let i = 0; i <= data.length; i++) {
        console.log(_timeStamp.timeStampToDate(data[i].TimeStamp, 'D'));
        days_color.push({
          month: 'current', day: _timeStamp.timeStampToDate(data[i].TimeStamp, 'D'), color: 'white', background: app.globalData.moodList[parseInt(data[i].Mood)].color
        });
        that.setData({
          days_color
        })
      }
      that.setData({
        days_color
      })
    }, function () {

    }
    );

    _heWeather.getWeather(function(weathers){
     mainPageViewModel.setData({
       weather: weathers.HeWeather6[0].now.cond_txt
     })
      app.globalData.todayWeather = weathers.HeWeather6[0].now.cond_txt;
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    mainPageViewModel=this;
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})